home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
The World of Computer Software.iso
/
vsc92nov.zip
/
misc-prim.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-02
|
5KB
|
256 lines
/*
* misc-prim.c -- Implementation of miscellaneous Scheme primitives
*
* (C) m.b (Matthias Blume), Mon May 18 15:23:56 MET DST 1992, HUB/Ger
* Humboldt-University of Berlin, Germany
*/
# ident "@(#)misc-prim.c (C) M.Blume, Humboldt-Uni Berlin, 1.7"
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include "storage.h"
# include "Cont.h"
# include "Number.h"
# include "String.h"
# include "Boolean.h"
# include "Code.h"
# include "Primitive.h"
# include "type.h"
# include "tmpstring.h"
# include "mode.h"
# include "except.h"
# include "builtins.tab"
/* Primitive No. 0: */
void ScmVMTrapC (void)
{
reset ("VM Trap");
}
void ScmVMTrap (unsigned short cnt)
{
ScmVMTrapC ();
}
/* Primitive No. 1: */
void ScmVMErrorC (void)
{
reset ("tried to call error continuation");
}
void ScmVMError (unsigned short argcnt)
{
ScmVMErrorC ();
}
/* Primitive No. 2: */
void ScmVMGCStrategyC (void)
{
void *tmp = ScmPeek ();
long bound;
if (tmp == &ScmFalse)
reset ("user gc-strategy gives up");
if (ScmTypeOf (tmp) == ScmType (ExactNumber)) {
bound = ScmNumberToInt (tmp);
gc_set_min_heap_size (bound);
}
ScmRevertToFatherContinuation (1);
(void) ScmPop ();
}
void ScmVMGCStrategy (unsigned short argcnt)
{
fatal ("internal error (ScmVMGCStrategy called)");
}
/* Primitive No. 3: */
void ScmVMInterruptC (void)
{
ScmRevertToFatherContinuation (1);
(void) ScmPop ();
}
void ScmVMInterrupt (unsigned short argcnt)
{
fatal ("internal error (ScmVMInterrupt called)");
}
void ScmPrimitiveQuit (unsigned short argcnt)
{
int stat = EXIT_SUCCESS;
if (argcnt == 1)
stat = ScmNumberToInt (ScmPop ());
else if (argcnt > 1)
error ("wrong argcnt (%u) for primitive procedure quit", (unsigned) argcnt);
exit (stat);
}
# define DUMP_FILENAME_LEN 256
/*ARGSUSED*/
void ScmPrimitiveDump (unsigned short argcnt)
{
void *tmp;
ScmString *string;
FILE *fp;
char *filename;
tmp = ScmPop ();
if (ScmTypeOf (tmp) != ScmType (String))
error ("wrong argument type for primitive procedure dump: %w", tmp);
string = tmp;
filename = tmpstring (string->array, string->length);
if ((fp = fopen (filename, "wb")) == NULL)
error ("cannot open file \"%s\" for dump", filename);
ScmPush (&ScmFalse);
ScmPushContinuation (1);
dump_storage (fp);
fclose (fp);
ScmCC = *ScmCC.father;
ScmSetTop (&ScmTrue);
}
/*ARGSUSED*/
void ScmPrimitiveExecuteAsm (unsigned short argcnt)
{
void *proc = ScmAsm (ScmPeek ());
ScmSetTop (proc);
ScmCC.call_again = 1;
}
/*ARGSUSED*/
void ScmPrimitiveDefineAsm (unsigned short argcnt)
{
(void) ScmAsmDcl (ScmPeek ());
ScmSetTop (&ScmTrue);
}
/*ARGSUSED*/
void ScmPrimitiveSystem (unsigned short argcnt)
{
ScmString *string;
int status;
string = ScmPeek ();
if (ScmTypeOf (string) != ScmType (String))
error ("bad arg to primitive procedure system: %w", string);
status = system (tmpstring (string->array, string->length));
ScmSetTop (status == EXIT_SUCCESS ? &ScmTrue : &ScmFalse);
}
static void call_with_mode (int mode_id)
{
void *mode;
mode = ScmPop ();
ScmPushPrimitiveContinuation (mode, 1);
ScmSetMode (mode_id, ScmCC.environ);
ScmPush (ScmCPop (ScmCC.father));
ScmCC.call_again = 1;
}
/*ARGSUSED*/
void ScmPrimWithErrorHandler (unsigned short argcnt)
{
call_with_mode (SCM_ERROR_HANDLER_MODE);
}
/*ARGSUSED*/
void ScmPrimWithGCStrategy (unsigned short argcnt)
{
call_with_mode (SCM_GC_STRATEGY_MODE);
}
/*ARGSUSED*/
void ScmPrimWithIntHandler (unsigned short argcnt)
{
call_with_mode (SCM_INTERRUPT_MODE);
}
void ScmPrimWithSomethingC (void)
{
ScmRevertToFatherContinuation (1);
}
/*ARGSUSED*/
void ScmPrimitiveInspect (unsigned short argcnt)
{
ScmContinuation *cont;
ScmVector *vect;
void *tmp;
long n;
unsigned long nxt_stat;
unsigned short stack_top, call_again;
cont = ScmPop ();
tmp = ScmPeek ();
ScmSetTop (cont);
n = ScmNumberToInt (tmp);
cont = ScmPeek ();
while (n-- > 0 && ScmTypeOf (cont) == ScmType (Continuation))
cont = cont->father;
if (ScmTypeOf (cont) != ScmType (Continuation))
ScmSetTop (&ScmFalse);
else {
ScmSetTop (cont);
nxt_stat = cont->nxt_stat;
stack_top = cont->stack_top;
call_again = cont->call_again;
vect = NewScmVector (8);
cont = ScmPeek ();
vect->array [0] =
(cont->code == NULL) ? &ScmFalse : cont->code->proc_name;
vect->array [2] = cont->environ;
vect->array [3] = cont->constants;
vect->array [4] = cont->stack;
vect->array [6] = cont->shared ? &ScmTrue : &ScmFalse;
ScmSetTop (vect);
tmp = (cont->code == NULL)
? GetScmPrimitive (nxt_stat)
: ScmIntToExactNumber (nxt_stat);
vect = ScmPeek ();
vect->array [1] = tmp;
tmp = ScmIntToExactNumber (stack_top);
vect = ScmPeek ();
vect->array [5] = tmp;
tmp = ScmIntToExactNumber (call_again);
vect = ScmPeek ();
vect->array [7] = tmp;
}
}
/*ARGSUSED*/
void ScmPrimitiveError (unsigned short argcnt)
{
error ("%d", ScmPeek ());
}
# if (1000 < CLOCKS_PER_SEC)
# define CLK2MS(clk) ((clk)/(CLOCKS_PER_SEC/1000))
# else
# define CLK2MS(clk) (((clk)*1000)/CLOCKS_PER_SEC)
# endif
/*ARGSUSED*/
void ScmPrimitiveClock (unsigned short argcnt)
{
void *tmp = ScmIntToExactNumber ((long) CLK2MS (clock ()));
ScmPush (tmp);
}
/*ARGSUSED*/
void ScmPrimitiveGcClock (unsigned short argcnt)
{
void *tmp = ScmIntToExactNumber ((long) CLK2MS (total_gc_clock ()));
ScmPush (tmp);
}